﻿/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * 
*     ___                              __                 _       __              __     __       *
*    /   |   _____ ____   ___   _____ /_/____   ____     | |     / /____   _____ / /____/ /_____  *
*   / /| |  / ___// __ \ / _ \ / ___// // __ \ / __ \    | | /| / // __ \ / ___// // __  // ___/  *
*  / ___ | _\_ \ / /_/ //  __// /   / // /_/ // / / /    | |/ |/ // /_/ // /   / // /_/ /_\_ \    *
* /_/  |_|/____// ____/ \___//_/   /_/ \____//_/ /_/     |__/|__/ \____//_/   /_/ \____//____/    *
*              /_/                                                                                *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Worlds.Application;
using Worlds.Application.Helpers;
using Worlds.Application.Configuration;

namespace Client.Framework
{
    /// <summary>
    /// Internal configuration logic, used to load and save configuration.
    /// </summary>
    public static class Configuration
    {
        /// <summary>
        /// Configuration loaded hook, raised when the configuration file(s) are loaded.
        /// </summary>
        public static event EventHandlers.EventHandler ConfigurationLoaded;
        /// <summary>
        /// Configuration saved hook, raised when the configuration file(s) are saved.
        /// </summary>
        public static event EventHandlers.EventHandler ConfigurationSaved;
        /// <summary>
        /// Configuration bound hook, called when configuration events are bound to methods.
        /// </summary>
        public static event EventHandlers.EventHandler ConfigurationBound;

        /// <summary>
        /// Loads the configuration object(s) for this applicaiton.
        /// </summary>
        public static void load()
        {
            /* Do not attempt to load the file unless it exists. */
            if (File.Exists("Client.config.xml"))
            {
                /* Break out the XMLHelper to help us deserialize. */
                Object XMLData = XMLHelper.load("Client.config.xml", typeof(ClientConfiguration));
                Objects.Configuration.set((ClientConfiguration)XMLData);
            }

            /* Attempt to fire the plugin hook. */
            if (ConfigurationLoaded != null)
                ConfigurationLoaded();
        }

        /// <summary>
        /// Saves the configuration object(s) for this applicaiton.
        /// </summary>
        public static void save()
        {
            /* Do not attempt to save if hte object is null. */
            if (Objects.Configuration != null)
            {
                /* Break out the XMLHelper to help us save. */
                XMLHelper.save("Client.config.xml", typeof(ClientConfiguration), Objects.Configuration);
            }

            /* Attempt to fire the plugin hook. */
            if (ConfigurationSaved != null)
                ConfigurationSaved();
        }

        /// <summary>
        /// Binds configuration events to methods for this applicaiton.
        /// </summary>
        public static void bind()
        {
            /* Attempt to fire the plugin hook. */
            if (ConfigurationBound != null)
                ConfigurationBound();
        }
    }
}
